home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Applications / PICSee Dust 1.01 / Quaternary Source / SavePicture.c < prev    next >
C/C++ Source or Header  |  1995-11-04  |  5KB  |  175 lines

  1. #include "SavePicture.h"
  2.  
  3. /*
  4.     This (handy?) little 'ol function saves a picture as a standard "PICT"
  5.     file. Note that barely any error-checking is done; you should add more
  6.     extensive checking for your own application(s). As an excuse for error
  7.     checking, this routine beeps when it encounters one and exits (quite
  8.     ungracefully, too).
  9.     
  10.     A special note concerning PICT files: A pict file is like any other
  11.     "data" file in that you write stuff to the data fork. However, the
  12.     pict file also contains a "special" 512-byte header. Most of the time
  13.     you can ignore this. So when writing to or reading from a pict file,
  14.     just skip the first 512 bytes (i.e. SetFPos(fileRefNum, fsFromStart, 512))
  15.     and all will be well. My experience is that almost all applications
  16.     ignore this 512-byte header (but keep it due to ancient history).
  17.     
  18.     By the way, the code herein is System 7 dependent. If you wish to
  19.     change to System 6 compatibility, you'll have to change the
  20.     StandardFileReply to an SFReply, etc. etc. etc.
  21.     (But why System 6?)
  22.     
  23.     
  24.     Version History:
  25.         1.1 Oct 31 95
  26.             Added creator argument, handling of "replace file?" situations.
  27.         1.2 Nov 2 95
  28.             Separated save picture into 2 routines (one with prompt, the
  29.             other doing the actual save) and added SavePictureResource.
  30.             Now a complete suite of PICT saving routines!
  31. */
  32.  
  33. Boolean SavePicture(
  34.     Handle    thePic,
  35.     OSType    creator,
  36.     Str31    fileName)  {
  37.  
  38.     StandardFileReply reply;
  39.     OSErr myErr;
  40.  
  41.     // Put up the standard save dialog
  42.     StandardPutFile("\pSave picture into:", fileName, &reply);
  43.  
  44.     if (reply.sfGood)  {
  45.         if (reply.sfReplacing) {
  46.             myErr = FSpDelete(&reply.sfFile);
  47.             if (myErr != noErr) {
  48.                 SysBeep(10);
  49.                 return(false);
  50.             }
  51.         }
  52.  
  53.         // Create the file.
  54.         myErr = FSpCreate(&reply.sfFile, creator, 'PICT', reply.sfScript);
  55.         if (myErr != noErr)  {
  56.             SysBeep(10);
  57.             return(false);
  58.         }
  59.         
  60.         return(SavePictureFile(&reply.sfFile, thePic));
  61.     }
  62.     else
  63.         return(false);
  64. } // END SavePicture
  65.  
  66. // ---------------------------------------------------------------------------
  67.  
  68. Boolean SavePictureFile(
  69.     FSSpec    *pictFile,
  70.     Handle    thePic) {
  71.  
  72.     OSErr myErr;
  73.     short fileRefNum;
  74.     long picSize;        // The size of our picture, give or take a header
  75.     
  76.  
  77.     // Open it up (the data fork, that is)...
  78.     myErr = FSpOpenDF(pictFile, fsCurPerm, &fileRefNum);
  79.     if (myErr != noErr)  {
  80.         SysBeep(10);
  81.         return(false);
  82.     }
  83.  
  84.     // Lock up the picture, since it may move when writing to
  85.     // our file. Also get the size of our picture. Then tell
  86.     // the File Manager to allocate enough space in the file
  87.     // for us, keeping in mind the 512-byte header...
  88.     HLock(thePic);
  89.     picSize = GetHandleSize(thePic);
  90.     picSize = picSize + 512;
  91.     myErr = SetEOF(fileRefNum, picSize);
  92.     if (myErr != noErr)  {
  93.         SysBeep(10);
  94.         return(false);
  95.     }
  96.  
  97.     // Alright, time to do the dirty deed (but first,
  98.     // since we don't want to write in the header, tell
  99.     // the File Manager to skip the first 512-bytes)
  100.     myErr = SetFPos(fileRefNum, fsFromStart, 512);
  101.     if (myErr != noErr)  {
  102.         SysBeep(10);
  103.         return(false);
  104.     }
  105.  
  106.     // OK, ok, here we are. Write to the file.
  107.     // Note: FSWrite expects a pointer to our data (i.e. the
  108.     // picture). So we'll have to dereference the
  109.     // picture handle.
  110.     picSize = GetHandleSize(thePic);
  111.     myErr = FSWrite(fileRefNum, &picSize, (Ptr)*thePic);
  112.     if (myErr != noErr)  {
  113.         SysBeep(10);
  114.         return(false);
  115.     }
  116.  
  117.     // Close it up. You may have to call FlushVol() or
  118.     // such to tell the File Manager to write the data
  119.     // to the file immediately (otherwise it waits a
  120.     // while...)
  121.     myErr = FSClose(fileRefNum);
  122.     if (myErr != noErr)  {
  123.         SysBeep(10);
  124.         return(false);
  125.     }
  126.     
  127.     return(true);
  128. } // END SavePictureFile
  129.  
  130. // ---------------------------------------------------------------------------
  131.  
  132. Boolean SavePictureResource(
  133.     PicHandle    thePic,
  134.     short        fileRefNum,
  135.     short        resID,
  136.     Str31        resName) {
  137.     
  138.     Handle picHdl;
  139.     short oldFileRefNum;
  140.     
  141.     oldFileRefNum = CurResFile();
  142.     UseResFile(fileRefNum);
  143.     
  144.     // Make sure resource doesn't already exist (if it does, delete it)
  145.     do {
  146.         picHdl = Get1Resource('PICT', resID);
  147.         if (picHdl != NULL) {
  148.             RemoveResource((Handle)picHdl);
  149.             DisposeHandle((Handle)picHdl);
  150.         }
  151.     } while (picHdl != NULL);
  152.  
  153.     if (resName != NULL)
  154.         AddResource((Handle)thePic, 'PICT', resID, resName);
  155.     else
  156.         AddResource((Handle)thePic, 'PICT', resID, "\p");
  157.     if (ResError() != noErr) {
  158.         UseResFile(oldFileRefNum);
  159.         return(false);
  160.     }
  161.  
  162.     WriteResource((Handle)thePic);
  163.     if (ResError() != noErr) {
  164.         UseResFile(oldFileRefNum);
  165.         return(false);
  166.     }
  167.     DetachResource((Handle)thePic);
  168.     if (ResError() != noErr) {
  169.         UseResFile(oldFileRefNum);
  170.         return(false);
  171.     }
  172.  
  173.     UseResFile(oldFileRefNum);
  174.     return(true);
  175. } // END SavePictureResource